package net.minecraft.src;

import java.util.List;
import net.minecraft.client.Minecraft;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

public class GuiEditRecTransStamp extends GuiScreen
{
    protected String screenTitle;
    protected TileReceiverTransmitterCommon tileRecTrans;
    private int updateCounter;
    private int editLine;
    private static final String allowedCharacters;

    public GuiEditRecTransStamp(TileReceiverTransmitterCommon inTileRecTrans)
    {
        screenTitle = "Edit sign message:";
        editLine = 0;
        tileRecTrans = inTileRecTrans;
    }

    public void initGui()
    {
        controlList.clear();
        Keyboard.enableRepeatEvents(true);
        controlList.add(new GuiButton(0, width / 2 - 100, height / 4 + 120, "Done"));
    }

    public void onGuiClosed()
    {
        Keyboard.enableRepeatEvents(false);
        if (mc.theWorld.isRemote)
        {
        	Packet230ModLoader  packet = new Packet230ModLoader ();

        	tileRecTrans.saveIntoPacket(packet);

            ModLoaderMp.sendPacket( ModLoaderMp.getModInstance(mod_AsPj.class), packet);
        }
    }

    public void updateScreen()
    {
        updateCounter++;
    }

    protected void actionPerformed(GuiButton guibutton)
    {
        if (!guibutton.enabled)
        {
            return;
        }
        if (guibutton.id == 0)
        {
        	tileRecTrans.onInventoryChanged();
            mc.displayGuiScreen(null);
        }
    }

    protected void keyTyped(char c, int i)
    {
        if (i == 200)
        {
            editLine = editLine - 1 & 3;
        }
        if (i == 208 || i == 28)
        {
            editLine = editLine + 1 & 3;
        }
        if (i == 14 && tileRecTrans.stampKey[editLine].length() > 0)
        {
        	tileRecTrans.stampKey[editLine] = tileRecTrans.stampKey[editLine].substring(0, tileRecTrans.stampKey[editLine].length() - 1);
        }
        if (allowedCharacters.indexOf(c) >= 0 && tileRecTrans.stampKey[editLine].length() < 15)
        {
        	tileRecTrans.stampKey[editLine] += c;
        }
    }

    public void drawScreen(int i, int j, float f)
    {
        drawDefaultBackground();
        drawCenteredString(fontRenderer, screenTitle, width / 2, 40, 0xffffff);
        GL11.glPushMatrix();
        GL11.glTranslatef(width / 2, 120.0F, 100F);
        float f1 = 93.75F;
        GL11.glScalef(-f1, -f1, -f1);
        
        GL11.glRotatef(180F, 0.0F, 0.0F, 1.0F);
        GL11.glRotatef(-80F, 1.0F, 0.0F, 0.0F);
        Block block = tileRecTrans.getBlockType();

        {
            int k = tileRecTrans.getBlockMetadata();
            float f3 = 0.0F;
            if (k == 2)
            {
                f3 = 180F;
            }
            if (k == 4)
            {
                f3 = 90F;
            }
            if (k == 5)
            {
                f3 = -90F;
            }
            GL11.glRotatef(f3, 0.0F, 1.0F, 0.0F);
            GL11.glTranslatef(0.0F, -1.0625F, 0.0F);
        }
        if ((updateCounter / 6) % 2 == 0)
        {
        	tileRecTrans.lineBeingEdited = editLine;
        }
  
        
        
       TileEntityRenderer.instance.renderTileEntityAt(tileRecTrans, -0.5D, -0.75D, -0.5D, 0.0F);

        tileRecTrans.lineBeingEdited = -1;
        GL11.glPopMatrix();
        super.drawScreen(i, j, f);
    }

    static
    {
        allowedCharacters = ChatAllowedCharacters.allowedCharacters;
    }
}
